package com.hkws.hkws.service;

import com.alibaba.fastjson.JSONObject;
import com.hkws.hkws.API.RootApi;
import com.hkws.hkws.mapper.RootinfoMapper;
import com.hkws.hkws.pojo.Rootinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class RootInfoInsert {
    @Autowired
    RootinfoMapper rootinfoMapper;
    @Scheduled(fixedRate = 1000)
    void execute()
        {
            String url = "https://open-api.yunjiai.cn/v1/robot/info?accessKeyId=dhz6Ra7Jo6qLD6me&deviceId=896002959698169856&signature=6clyx6skjzQcmwHECbNd8H8zCS8%3D&signatureNonce=1&storeId=202189105671016556095847096576&timestamp=1";
            RootApi rootTestApi = new RootApi();
            Rootinfo rootInfo =new Rootinfo();
            String s1 = rootTestApi.loadJson(url);
            if(s1!=null) {
                JSONObject rootjson = JSONObject.parseObject(s1);
                JSONObject liststate = (JSONObject) rootjson.get("data");
                Double powerPercent = Double.parseDouble(liststate.get("powerPercent").toString());
                int currentFloor = (int) liststate.get("currentFloor");
                String status = liststate.get("status").toString();
                JSONObject position = (JSONObject) liststate.get("position");
                Double positionX = Double.parseDouble(position.get("x").toString());
                Double positionY = Double.parseDouble(position.get("y").toString());
                Double positionTheta = Double.parseDouble(position.get("theta").toString());
                JSONObject orientation = (JSONObject) liststate.get("orientation");
                Double orientationX = Double.parseDouble(orientation.get("x").toString());
                Double orientationY = Double.parseDouble(orientation.get("y").toString());
                Double orientationZ = Double.parseDouble(orientation.get("z").toString());
                Double orientationW = Double.parseDouble(orientation.get("w").toString());
                rootInfo.setCurrentFloor(currentFloor);
                rootInfo.setOrientationW(orientationW);
                rootInfo.setOrientationX(orientationX);
                rootInfo.setOrientationY(orientationY);
                rootInfo.setOrientationZ(orientationZ);
                rootInfo.setPositionX(positionX);
                rootInfo.setPositionY(positionY);
                rootInfo.setPositionTheta(positionTheta);
                rootInfo.setPowerPercent(powerPercent);
                rootInfo.setStatus(status);
                rootInfo.setDate(new Date());
                System.out.println("data"+new Date());
                int insert = rootinfoMapper.insert(rootInfo);
                if (insert == 1)
                    System.out.println("成功");
                else
                    System.out.println("失败");


            }
        }
    }
